﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace InventoryMIS2019
{
    public partial class _Supplier : InventoryMIS2019.Templates.BaselnfoForm
    {
        private SupplierBLL bl;
        private Supplier c;
        private Contact contact;
        private List<Supplier> list;
        public _Supplier()
        {
            InitializeComponent();
            bl = new SupplierBLL();
            dgvSupplier.AutoGenerateColumns = false;
            BindData();
            ControlOpera(false);
        }
        private void BindData()
        {
            dgvSupplier.DataSource = null;//清空DataGridView的数据源
            list = bl.GetList();
            dgvSupplier.DataSource = list;//将list集合数据绑定到控件dgvCustomer作为数据源
        }
        /// <summary>
        /// 定义单击 “新增/修改/删除”按钮式，GroupBox控件中TextBox的可操作性
        /// </summary>
        /// <param name="value"></param>
        private void ControlOpera(bool value)
        {
            txtSupplierID.Enabled = value;
            txtSupplierName .Enabled = value;
            txtAddress .Enabled = value;
            txtZipCode.Enabled = value;
            txtTel.Enabled = value;
            txtFax.Enabled = value;
            txtBankName.Enabled = value;
            txtBankAccount.Enabled = value;
            txtContacter.Enabled = value;
            txtEmail.Enabled = value;
        }

        /// <summary>
        /// 数据验证
        /// </summary>
        /// <returns></returns>
        private string Validation()
        {
            if (txtSupplierID.Text.Trim() == "")
            {
                txtSupplierID.Focus();//将光标定位值产品类型编号输入框
                return "供应商编号不能为空！";
            }
            if (txtSupplierName.Text.Trim() == "")
            {
                txtSupplierName.Focus();//将光标定位值产品类型名称输入框
                return "供应商名称不能为空！";
            }
            if (txtSpellingCode .Text.Trim() == "")
            {
                txtSpellingCode.Focus();//将光标定位值产品类型编号输入框
                return "拼音不能为空！";
            }
            if (txtAddress.Text.Trim() == "")
            {
                txtAddress.Focus();//将光标定位值产品类型名称输入框
                return "地址不能为空！";
            }
            if (txtZipCode.Text.Trim() == "")
            {
                txtZipCode.Focus();//将光标定位值产品类型编号输入框
                return "邮编不能为空！";
            }
            if (txtTel.Text.Trim() == "")
            {
                txtTel.Focus();//将光标定位值产品类型名称输入框
                return "电话不能为空！";
            }
            if (txtBankName.Text.Trim() == "")
            {
                txtBankName.Focus();//将光标定位值产品类型编号输入框
                return "银行名称不能为空！";
            }
            if (txtBankAccount.Text.Trim() == "")
            {
                txtBankAccount.Focus();//将光标定位值产品类型编号输入框
                return "银行账号不能为空！";
            }
            if (txtContacter.Text.Trim() == "")
            {
                txtContacter.Focus();//将光标定位值产品类型名称输入框
                return "联系人不能为空！";
            }
            return "";
        }
        /// <summary>
        /// 将业务实体内容显示到TextBox控件中
        /// </summary>
        private void ShowInfo()
        {
            txtSupplierID.Text = c.SupplierID .Trim();
            txtSupplierName.Text = c.SupplierName .Trim();
            txtSpellingCode.Text = c.ContactInfo.SpellingCode.Trim();
            txtAddress.Text = c.ContactInfo.Address.Trim();
            txtZipCode.Text = c.ContactInfo.ZipCode.Trim();
            txtTel.Text = c.ContactInfo.Tel.Trim();
            txtFax .Text = c.ContactInfo.Fax.Trim();
            txtBankName.Text = c.ContactInfo.BankName.Trim();
            txtBankAccount.Text = c.ContactInfo.BankAccount.Trim();
            txtContacter.Text = c.ContactInfo.Contacter.Trim();
            txtEmail .Text = c.ContactInfo.Email.Trim();
        }
        private Supplier SetValue()
        {
            c = new Supplier();
            c.SupplierID  = txtSupplierID.Text.Trim();
            c.SupplierName  = txtSupplierName.Text.Trim();
            contact = new Contact();
            contact.SpellingCode = txtSpellingCode.Text.Trim();
            contact.Address = txtAddress.Text.Trim();
            contact.ZipCode = txtZipCode.Text.Trim();
            contact.Tel = txtTel.Text.Trim();
            contact.Fax = txtFax.Text.Trim();
            contact.BankName = txtBankName.Text.Trim();
            contact.BankAccount = txtBankAccount.Text.Trim();
            contact.Contacter = txtContacter.Text.Trim();
            contact.Email = txtEmail.Text.Trim();
            c = new Supplier(c, contact);
            return c;
        }

        private void TemInventory_Click(object sender, EventArgs e)
        {
            string errorString = Validation();
            if (errorString.Length > 0) { MessageBox.Show(errorString, "系统提示"); return; }
            if (opera == Operation.INSERT)
            {
                SetValue();//获取控件值并完成对象赋值
                if (bl.Insert(c))
                {
                    BindData();//绑定DataGridView的数据源                    
                    Clear();//清空TextBox控件内容
                    MessageBox.Show("新增数据成功！", "系统提示");
                }
                else
                    MessageBox.Show("新增数据失败！", "系统提示");
            }
            if (opera == Operation.UPDATE)
            {
                if (c != null)
                {
                    SetValue();//获取控件值并完成对象赋值
                    if (bl.Update(c))
                    {
                        BindData();//绑定DataGridView的数据源
                        ShowInfo();

                        MessageBox.Show("成功更新数据！", "系统提示");
                    }
                    else
                        MessageBox.Show("更新数据失败！", "系统提示");
                }
                else
                    MessageBox.Show("请选择需要修改的记录", "系统提示");
            }
            if (opera == Operation.DELETE)
            {
                if (c != null)
                {
                    SetValue();//获取控件值并完成对象赋值
                    if (bl.Delete(c))
                    {
                        BindData();//绑定DataGridView的数据源

                        Clear();//清空TextBox控件内容
                        MessageBox.Show("成功删除数据！", "系统提示");
                    }
                    else
                        MessageBox.Show("删除数据失败！", "系统提示");
                }
                else
                    MessageBox.Show("请选择需要删除的记录", "系统提示");
            }
        }

        private void TemNew_Click(object sender, EventArgs e)
        {
            ControlOpera(true);
            txtSpellingCode.Enabled = false;
        }

        private void TemModify_Click(object sender, EventArgs e)
        {
            ControlOpera(true);
            txtSupplierID .Enabled = false;
            txtSpellingCode.Enabled = false;
            if (c != null)
                ShowInfo();
        }

        private void TemDelete_Click(object sender, EventArgs e)
        {
            if (c != null)
                ShowInfo();
        }

        private void dgvSupplier_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (dgvSupplier.Columns[e.ColumnIndex].Name == "SpellingCode")
                e.Value = ((Contact)e.Value).SpellingCode;
            if (dgvSupplier.Columns[e.ColumnIndex].Name == "Address")
                e.Value = ((Contact)e.Value).Address;
            if (dgvSupplier.Columns[e.ColumnIndex].Name == "ZipCode")
                e.Value = ((Contact)e.Value).ZipCode;
            if (dgvSupplier.Columns[e.ColumnIndex].Name == "Tel")
                e.Value = ((Contact)e.Value).Tel;
            if (dgvSupplier.Columns[e.ColumnIndex].Name == "Fax")
                e.Value = ((Contact)e.Value).Fax;
            if (dgvSupplier.Columns[e.ColumnIndex].Name == "BankName")
                e.Value = ((Contact)e.Value).BankName;
            if (dgvSupplier.Columns[e.ColumnIndex].Name == "BankAccount")
                e.Value = ((Contact)e.Value).BankAccount;
            if (dgvSupplier.Columns[e.ColumnIndex].Name == "Contacter")
                e.Value = ((Contact)e.Value).Contacter;
            if (dgvSupplier.Columns[e.ColumnIndex].Name == "Email")
                e.Value = ((Contact)e.Value).Email;
        }

        private void dgvSupplier_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvSupplier.CurrentRow.Selected = true;
            c = list[e.RowIndex];
            if (opera == Operation.UPDATE || opera == Operation.DELETE)
                ShowInfo();
        }

        private void txtSupplierName_TextChanged(object sender, EventArgs e)
        {
            txtSpellingCode.Text = PY.HZToSpell(txtSupplierName.Text.Trim());
        }
    }
}
